package oop.ex3.usercommands;

import java.util.LinkedList;
import oop.ex3.filemanager.FileManager;
import oop.ex3.filemanager.UploadThread;
import oop.ex3.protocol.GoAway;
import oop.ex3.protocol.Goodbye;
import oop.ex3.sharedclasses.IPaddress;

public class Quit extends UserMessage{

	public static final String COMMAND="QUIT";
	//private LinkedList<UploadThread> _uploads;
	
	//public Quit(FileManager fileManager, LinkedList<UploadThread> uploads) {
	public Quit(FileManager fileManager){
		super(fileManager);
		//_uploads=uploads;
	}

	/**
	 * Runs the Quit UserMessage.
	 * sends GoodBye to all known NameServers, and closes the FileManager.
	 */
	public boolean run(){
		
		//TODO check if disableUploads is good, or I can just prevent creation of
		// new threads.
		/*_fileManager.disableUploads();
		
		for (UploadThread thread : _uploads){
			try {
				thread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		_fileManager.setOffline(); //sets offline
		_fileManager.finishAllUploads(); //finished uploads
		
		LinkedList<IPaddress> nameServers= this._fileManager.getNameServerList();
		Goodbye disconnector=new Goodbye();
		for (IPaddress nameServer : nameServers){
			disconnector.runAsClient(_fileManager, nameServer, null);
		}
		System.out.println("Bye-bye!");
		return SUCCESS;
	}
}
